/*
*  Copyright 2019-2025 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.account.service.Impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.account.domain.AccountAa03;
import me.zhengjie.modules.account.domain.AccountAc50a5;
import me.zhengjie.modules.account.domain.dto.AccountAa03QueryCriteria;
import me.zhengjie.modules.account.mapper.AccountAa03Mapper;
import me.zhengjie.modules.account.service.AccountAa03Service;
import me.zhengjie.modules.logs.domain.BusinessLog;
import me.zhengjie.modules.logs.service.BusinessLogService;
import me.zhengjie.modules.sequence.domain.Sequence;
import me.zhengjie.modules.sequence.service.SequenceService;
import me.zhengjie.tool.DateUtil;
import me.zhengjie.tool.ValidateUtil;
import me.zhengjie.utils.FileUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import me.zhengjie.utils.SecurityUtils;
import me.zhengjie.vo.Constant;
import me.zhengjie.vo.YHConstants;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import me.zhengjie.utils.PageUtil;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.*;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import me.zhengjie.utils.PageResult;

/**
* @description 服务实现
* @author lzw
* @date 2025-04-23
**/
@Service
@RequiredArgsConstructor
public class AccountAa03ServiceImpl extends ServiceImpl<AccountAa03Mapper, AccountAa03> implements AccountAa03Service {

    private final AccountAa03Mapper accountAa03Mapper;
    private final SequenceService sequenceService;
    private final BusinessLogService businessLogService;

    @Override
    public PageResult<AccountAa03> queryAll(AccountAa03QueryCriteria criteria, Page<Object> page){
        Date date1 =null;

            if(!ValidateUtil.isEmpty(criteria.getAae003start())){

                // 1. 解析为 Instant（UTC 时间）
                Instant instant = Instant.parse(criteria.getAae003start());

                // 2. 转换为本地时间（可选）
                ZonedDateTime localTime = instant.atZone(ZoneId.systemDefault());

                // 3. 定义输出格式
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy");

                // 4. 格式化输出
                String formattedDate = localTime.format(formatter);
                criteria.setAae003start(formattedDate);
            }


        return PageUtil.toPage(accountAa03Mapper.findAll(criteria, page));
    }

    @Override
    public List<AccountAa03> queryAll(AccountAa03QueryCriteria criteria){
        return accountAa03Mapper.findAll(criteria);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(AccountAa03 resources) throws IOException {
        AccountAa03 accountAa03 = new AccountAa03();

        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMdd");
        Sequence sequence_aaz153 = new Sequence();
        sequence_aaz153.setName("aaz153");


        String currentUsername = SecurityUtils.getCurrentUsername();
        accountAa03.setAae011(currentUsername);
        accountAa03.setAae036(new BigDecimal(yyyyMMddHHmmss));
        accountAa03.setAab359("530111");
        accountAa03.setAaz153(sequenceService.nextSequence(sequence_aaz153));
        String aaa032="";
        switch (resources.getAaa030().toString()){
            case Constant.AAA030_80_SBDNJZLL:
                aaa032="社保当年记账利率";
                break;
            case Constant.AAA030_81_SBDNJZLL:
                aaa032="社保当年记账利率(机关事业)";
                break;
            case Constant.AAA030_90_SBJZLL:
                aaa032="社保历年利率";
                break;
            case Constant.AAA030_91_SBLNJZLL:
                aaa032="社保历年利率（机关事业）";
                break;
            default:
        }
        accountAa03.setAaa030(resources.getAaa030().toString());
        accountAa03.setAaa032(aaa032);
        accountAa03.setAaa031(new BigDecimal(resources.getAaa031().toString()));
        accountAa03.setAae013( (ValidateUtil.isEmpty(resources.getAae013())?"":resources.getAae013().toString()));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");


        accountAa03.setAae041(resources.getAae041());
        accountAa03.setAae042(ValidateUtil.isEmpty(resources.getAae042())?null:resources.getAae042());
        accountAa03Mapper.insert(accountAa03);

        //记录日志
        saveLog(YHConstants.AAA121_020201);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(AccountAa03 resources) throws IOException {

        AccountAa03 accountAa03 = new AccountAa03();

        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMdd");

        String currentUsername = SecurityUtils.getCurrentUsername();


        String aaa032="";
        switch (resources.getAaa030().toString()){
            case Constant.AAA030_80_SBDNJZLL:
                aaa032="社保当年记账利率";
                break;
            case Constant.AAA030_81_SBDNJZLL:
                aaa032="社保当年记账利率(机关事业)";
                break;
            case Constant.AAA030_90_SBJZLL:
                aaa032="社保历年利率";
                break;
            case Constant.AAA030_91_SBLNJZLL:
                aaa032="社保历年利率（机关事业）";
                break;
            default:
        }
        accountAa03.setAaz153(resources.getAaz153().toString());
        accountAa03.setAaa030(resources.getAaa030().toString());
        accountAa03.setAaa032(aaa032);
        accountAa03.setAaa031(new BigDecimal(resources.getAaa031().toString()));


        accountAa03.setAae041(resources.getAae041());
        accountAa03.setAae042(ValidateUtil.isEmpty(resources.getAae042())?null:resources.getAae042());
        accountAa03.setAae013( (ValidateUtil.isEmpty(resources.getAae013())?"":resources.getAae013().toString()));
        accountAa03.setAae011(currentUsername);
        accountAa03.setAae036(new BigDecimal(yyyyMMddHHmmss));
        accountAa03.setAab359("530111");

        updateById(accountAa03);

        //记录日志
        saveLog(YHConstants.AAA121_020202);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(List<String> ids) throws IOException {
        accountAa03Mapper.deleteBatchIds(ids);
        //记录日志
        saveLog(YHConstants.AAA121_020203);
    }

    @Override
    public void download(List<AccountAa03> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (AccountAa03 accountAa03 : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("利率名称", accountAa03.getAaa032());
            map.put("利率类型", accountAa03.getAaa030());
            map.put("利率", accountAa03.getAaa031());
            map.put("备注", accountAa03.getAae013());
            map.put("经办人", accountAa03.getAae011());
            map.put("经办时间", accountAa03.getAae036());
            map.put("开始日期", accountAa03.getAae041());
            map.put("终止日期", accountAa03.getAae042());
            map.put(" aab359",  accountAa03.getAab359());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public boolean save(AccountAa03 entity) {
        return super.save(entity);
    }



    public void saveLog(String aaa121) throws IOException {
        String yyyyMMddHHmmss = DateUtil.getCurrentTime("yyyyMMddHHmmss");

        //写日志
        Map map= new HashMap<>();

        map.put("aaz002",sequenceService.nextSequence(new Sequence("aaz002")));
        map.put("aaa121", aaa121);
        map.put("aab001","");
        map.put("aab004","");
        map.put("aac001","");
        map.put("aac002","");
        map.put("aac003","");
        map.put("aae140", Constant.AAE140_120_JGSYDWGZRYJBYLBX);
        map.put("aae016",Constant.AAE016_1_SHTG);
        map.put("aae011", SecurityUtils.getCurrentUsername());
        map.put("aae036",yyyyMMddHHmmss);
        map.put("aae014",SecurityUtils.getCurrentUsername());
        map.put("aae015",yyyyMMddHHmmss);
        map.put("aab359","530111");

        //记录日志信息
        BusinessLog businessLog = BusinessLog.singleLog(map);
        businessLogService.save(businessLog);

    }
}
